﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussines;
using System.Data.SqlClient;
using System.IO;

namespace ownx
{
    public partial class wfPartner : System.Web.UI.Page
    {
        clPARTNER _clPARTNER;
        clTIPUS_LIST _clTipusok;
        clIRSZ_LIST _clIRSZ;
        TBLObj blObj = null;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            string Err = "";
            
            if (Session["BL"] != null)
            {
                blObj = (TBLObj)Session["BL"];
            }
            //TBLObj blObj = new TBLObj("Test", Session["CONSTR"].ToString(), (clFIELDINFO_LIST)Session["FIELDINFO"]);            

            _clTipusok = new clTIPUS_LIST(blObj, "PRT");

            _clIRSZ = new clIRSZ_LIST(blObj);


            //ha ez az első meghívás
            if (!(Page.IsPostBack))
            {
                FillCombos();

                tbPRT_ID.Text = "-1";
                tbCMB_ID1.Text = "-1";
                tbCMB_ID2.Text = "-1";
                tbEML_ID.Text = "-1";
                tbTLF_ID1.Text = "-1";
                tbTLF_ID2.Text = "-1";
                tbCMB_IRS_ID1.Text = "-1";
                tbCMB_IRS_ID2.Text = "-1";
            }



            lStatus.Text = "SAVED";

            if (Convert.ToInt16(Session["PRT_ID"]) == -1)
            {
                lStatus.Text = "NEW";
            }

            if ((!(Page.IsPostBack)) && (Convert.ToInt16(Session["PRT_ID"]) != -1))
            {
                if (Convert.ToInt16(Session["PRT_ID"]) == -1)
                    lStatus.Text = "NEW";
                _clPARTNER = new clPARTNER(blObj, Convert.ToInt16(Session["PRT_ID"]));
                _clPARTNER.UpdateModified(ref Err);
                DataInit();
            }
            else
            {
                if (!(Page.IsPostBack))
                    ddlEv.SelectedValue = DateTime.Now.Year.ToString();
                _clPARTNER = new clPARTNER(blObj);
            }
        }

        private void FillCombos()
        {
            foreach (clTIPUS akt in _clTipusok.lTIPUS)
            {
                ListItem li = new ListItem(akt.fTPS_MEGNEVEZES, akt.fTPS_TIPUSKOD.ToString());

                ddlPRT_TIPUS.Items.Add(li);
            }

            for (int i = 1930; i < 2020; ++i)
            {
                ListItem li2 = new ListItem(i.ToString());
                ddlEv.Items.Add(li2);
            }
            
        }

        private void DataInit()
        {
            
            ddlPRT_TIPUS.SelectedValue = _clPARTNER.fPRT_TIPUS.ToString();

            if ((_clPARTNER.fPRT_NEM.ToString() == "F") || (_clPARTNER.fPRT_NEM.ToString() == "N"))
                ddlPRT_NEM.SelectedValue = _clPARTNER.fPRT_NEM.ToString();

            tbPRT_NEV1.Text = _clPARTNER.fPRT_NEV1;
            tbPRT_NEV2.Text = _clPARTNER.fPRT_NEV2;
            tbPRT_ID.Text = _clPARTNER.fPRT_ID.ToString();
            calPRT_SZUL_DATUM.SelectedDate = _clPARTNER.fPRT_SZUL_DATUM;
            calPRT_SZUL_DATUM.VisibleDate = _clPARTNER.fPRT_SZUL_DATUM;
            ddlPRT_NEM.SelectedValue = _clPARTNER.fPRT_NEM;
            if (_clPARTNER.lPARTNER_EMAIL.lPARNTER_MAIL.Count() > 0)
            {
                tbEML_MAILCIM.Text = _clPARTNER.lPARTNER_EMAIL.lPARNTER_MAIL[0].fEML_MAILCIM;
                tbEML_ID.Text = _clPARTNER.lPARTNER_EMAIL.lPARNTER_MAIL[0].fEML_ID.ToString();
            }
            else
            {
                tbEML_MAILCIM.Text = "";
                tbEML_ID.Text = "-1";
            }

            tbTLF_ID1.Text = "-1";
            tbTLF_ID2.Text = "-1";

            if (_clPARTNER.lPARTNER_TELEFON.lPARNTER_TELEFON.Count() > 0)
            {
                foreach (clPARTNER_TELEFON akt in _clPARTNER.lPARTNER_TELEFON.lPARNTER_TELEFON)
                {
                    if ((akt.fTLF_TIPUS == 0) && (tbTLF_ID1.Text == "-1"))
                    {
                        tbTLF_ID1.Text = akt.fTLF_ID.ToString();
                        tbTLF_SZAM1.Text = akt.fTLF_SZAM;
                    }

                    if ((akt.fTLF_TIPUS == 1) && (tbTLF_ID2.Text == "-1"))
                    {
                        tbTLF_ID2.Text = akt.fTLF_ID.ToString();
                        tbTLF_SZAM2.Text = akt.fTLF_SZAM;
                    }
                }
            }

            tbCMB_ID1.Text = "-1";
            tbCMB_ID2.Text = "-1";

            tbCMB_IRS_ID1.Text = "-1";
            tbCMB_IRS_ID2.Text = "-1";

            if (_clPARTNER.lPARTNER_CIM.lPARTNER_CIM.Count() > 0)
            {
                foreach (clPARTNER_CIM akt in _clPARTNER.lPARTNER_CIM.lPARTNER_CIM)
                {
                    if ((akt.fCMB_TIPUS == 0) && (tbCMB_ID1.Text == "-1"))
                    {
                        tbCMB_IRSZ_SZAM1.Text = akt.fIRS_SZAM;
                        tbCMB_ID1.Text = akt.fCMB_ID.ToString();
                        tbCMB_IRS_ID1.Text = akt.fCMB_IRS_ID.ToString();
                        tbCMB_VAROS1.Text = akt.fCMB_VAROS;
                        tbCMB_UTCA1.Text = akt.fCMB_UTCA;
                    }

                    if ((akt.fCMB_TIPUS == 1) && (tbCMB_ID2.Text == "-1"))
                    {
                        tbCMB_IRSZ_SZAM2.Text = akt.fIRS_SZAM;
                        tbCMB_ID2.Text = akt.fCMB_ID.ToString();
                        tbCMB_IRS_ID2.Text = akt.fCMB_IRS_ID.ToString();
                        tbCMB_VAROS2.Text = akt.fCMB_VAROS;
                        tbCMB_UTCA2.Text = akt.fCMB_UTCA;
                        
                    }
                }
            }
            if (_clPARTNER.fPRT_SZUL_DATUM.Year.ToString() != "")
            {
                ddlEv.SelectedValue = _clPARTNER.fPRT_SZUL_DATUM.Year.ToString();
            }
            else
            {
                ddlEv.SelectedValue = DateTime.Now.Year.ToString();
            }
            ddlPRT_TIPUS_SelectedIndexChanged(ddlPRT_TIPUS, null);
        }

        protected void bMent_Click(object sender, EventArgs e)
        {
            string oErr = "";
            _clPARTNER.fPRT_TIPUS = Convert.ToInt32 (ddlPRT_TIPUS.SelectedValue);
            _clPARTNER.fPRT_ID = Convert.ToInt32( tbPRT_ID.Text );
            _clPARTNER.fPRT_NEV1 = tbPRT_NEV1.Text ;
            _clPARTNER.fPRT_NEV2 = tbPRT_NEV2.Text ;
            _clPARTNER.fPRT_SZUL_DATUM = calPRT_SZUL_DATUM.SelectedDate;

            if (ddlPRT_NEM.Visible)
                _clPARTNER.fPRT_NEM = ddlPRT_NEM.SelectedValue;
            else
                _clPARTNER.fPRT_NEM = "C";
            
            //_clPARTNER.fPRT_TIPUS = ddlPRG_TIPUS.SelectedValue;
            _clPARTNER.fPRT_IGSZAM = "";

            clPARTNER_MAIL mail = new clPARTNER_MAIL(Convert.ToInt32(tbEML_ID.Text), Convert.ToInt32(tbPRT_ID.Text), tbEML_MAILCIM.Text, ""); 
            _clPARTNER.lPARTNER_EMAIL.lPARNTER_MAIL.Add(mail);

            clPARTNER_TELEFON tel = new clPARTNER_TELEFON(Convert.ToInt32(tbTLF_ID1.Text), 0, Convert.ToInt32(tbPRT_ID.Text), tbTLF_SZAM1.Text , "");
            _clPARTNER.lPARTNER_TELEFON.lPARNTER_TELEFON.Add(tel);
            clPARTNER_TELEFON tel2 = new clPARTNER_TELEFON(Convert.ToInt32(tbTLF_ID2.Text), 1, Convert.ToInt32(tbPRT_ID.Text), tbTLF_SZAM2.Text , "");
            _clPARTNER.lPARTNER_TELEFON.lPARNTER_TELEFON.Add(tel2);

            clORSZAG orsz = new clORSZAG(1, "teszt"); 
            clPARTNER_CIM cim = new clPARTNER_CIM(Convert.ToInt32(tbCMB_ID1.Text), Convert.ToInt32(tbPRT_ID.Text), orsz,  Convert.ToInt32(tbCMB_IRS_ID1.Text) , tbCMB_VAROS1.Text, tbCMB_UTCA1.Text, "", 0,tbCMB_IRSZ_SZAM1.Text );
            _clPARTNER.lPARTNER_CIM.lPARTNER_CIM.Add(cim);
            clPARTNER_CIM cim2 = new clPARTNER_CIM(Convert.ToInt32(tbCMB_ID2.Text), Convert.ToInt32(tbPRT_ID.Text), orsz, Convert.ToInt32(tbCMB_IRS_ID2.Text), tbCMB_VAROS2.Text, tbCMB_UTCA2.Text, "",1, tbCMB_IRSZ_SZAM1.Text);
            _clPARTNER.lPARTNER_CIM.lPARTNER_CIM.Add(cim2);

            _clPARTNER.fPRT_CREATED = DateTime.Now;
            _clPARTNER.fPRT_CREATED_BY = (int)Session["USR_ID"];
            _clPARTNER.fPRT_MODIFIED = DateTime.Now;
            _clPARTNER.fPRT_MODIFIED_BY = (int)Session["USR_ID"];

            _clPARTNER.fPRT_MEGJEGYZES = "";
            _clPARTNER.fPRT_NEM = ddlPRT_NEM.SelectedValue;

            if (_clPARTNER.SavePartner(ref oErr))
            {
                tbPRT_ID.Text = _clPARTNER.fPRT_ID.ToString();
                Session["PRT_ID"] = Convert.ToInt32( tbPRT_ID.Text );
                lStatus.Text = "SAVED";
            }
            else
            {
                lMessage.Text = oErr;
            }

            
        }

        protected void ddlPRT_TIPUS_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(ddlPRT_TIPUS.SelectedValue) < 50)
            {
                lPRT_NEV.Text = "Veztéknév:";
                lPRT_NEV2.Text = "Keresztnév:";
                lPRT_SZUL_DATUM.Text = "Szül. dátuma";
                lNem.Visible = true;
                ddlPRT_NEM.Visible = true;
            }
            else
            {
                lPRT_NEV.Text = "Megnevzés:";
                lPRT_NEV2.Text = "Rövidnév:";
                lPRT_SZUL_DATUM.Text = "Alapítás dátuma";
                lNem.Visible = false;
                ddlPRT_NEM.Visible = false;
            }
        }

        protected void tbCMB_IRSZ_SZAM1_TextChanged(object sender, EventArgs e)
        {
            clIRSZ temp;
            temp = _clIRSZ.GetItem(tbCMB_IRSZ_SZAM1.Text);
            tbCMB_VAROS1.Text = temp.fIRS_VAROS ;
            tbCMB_IRS_ID1.Text = temp.fIRS_ID.ToString();
            if (temp.fIRS_ID == -1)
            {
                lMessage.Text = "Nincs ilyen irányítószám a törzsadatokban!";
                tbCMB_IRSZ_SZAM1.Focus();
            }
            else
                lMessage.Text = "";
        }

        protected void tbCMB_IRSZ_SZAM2_TextChanged(object sender, EventArgs e)
        {
            clIRSZ temp;
            temp = _clIRSZ.GetItem(tbCMB_IRSZ_SZAM2.Text);
            tbCMB_VAROS2.Text = temp.fIRS_VAROS;
            tbCMB_IRS_ID2.Text = temp.fIRS_ID.ToString();
            if (temp.fIRS_ID == -1)
            {
                lMessage.Text = "Nincs ilyen irányítószám a törzsadatokban!";
                tbCMB_IRSZ_SZAM2.Focus();
            }
            else
                lMessage.Text = "";
        }

        protected void bVissza_Click(object sender, EventArgs e)
        {
            if (Session["PARENT_FORM"] != null)
            {
                Response.Redirect(Session["PARENT_FORM"].ToString());
            }
            else
            {
                Response.Redirect("wfPartnerChoice.aspx");
            }
        }

        protected void bAlapadatok_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
                Response.Redirect("wfPartner.aspx");
        }

        protected void bHitelek_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
            {
                lMessage.Text = "";
                Response.Redirect("wfHitelek.aspx");
            }
            else
            {
                lMessage.Text = "Alapadatok mentése kötelező!";
            }

        }

        protected void bMegj_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
            {
                lMessage.Text = "";
                Response.Redirect("wfMegjegyzes.aspx");
            }
            else
            {
                lMessage.Text = "Alapadatok mentése kötelező!";
            }
        }

        protected void bNyugdijak_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
            {
                lMessage.Text = "";
                Response.Redirect("wfNyugdij.aspx");
            }
            else
            {
                lMessage.Text = "Alapadatok mentése kötelező!";
            }
        }

        protected void bBefektetesek_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
            {
                lMessage.Text = "";
                Response.Redirect("wfBefektetesek.aspx");
            }
            else
            {
                lMessage.Text = "Alapadatok mentése kötelező!";
            }
        }

        protected void bEletBizt_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
            {
                lMessage.Text = "";
                Response.Redirect("wfBiztElet.aspx");
            }
            else
            {
                lMessage.Text = "Alapadatok mentése kötelező!";
            }
        }

        protected void bVagyonBizt_Click(object sender, EventArgs e)
        {
            bMent_Click(sender, e);
            if (lStatus.Text != "NEW")
            {
                lMessage.Text = "";
                Response.Redirect("wfBiztVagyon.aspx");
            }
            else
            {
                lMessage.Text = "Alapadatok mentése kötelező!";
            }
        }

        public bool ExcelExport(int iPRT_ID, int iAll, ref string oErr)
        {
            try
            {
                DataSet DS = new DataSet();
                SqlDataAdapter Adapter = new SqlDataAdapter();
                Adapter.SelectCommand = new SqlCommand("select * from dbo.FN_PARTNEREXPORT(" + iPRT_ID.ToString() + "," + blObj.USR_ID.ToString() + "," + iAll.ToString() + ")", blObj.sqlConnection);
                Adapter.Fill(DS, "PARTNER_EXP");

                StringWriter sw = new StringWriter();
                HtmlTextWriter wr = new HtmlTextWriter(sw);

                wr.RenderBeginTag(HtmlTextWriterTag.H1);
                wr.Write("Partnerek");
                wr.RenderEndTag();

                GridView grid = new GridView();
                grid.DataSource = DS;
                grid.DataBind();
                grid.HeaderStyle.Font.Bold = true;
                grid.RenderControl(wr);

                this.Response.Clear();
                this.Response.ContentType = "application/vnd.ms-excel";
                this.Response.Write(sw.ToString());
                this.Response.End();
                return true;
            }
            catch (Exception e)
            {
                string s = "Hiba a excel export közben!";
                Utilities.LogError(blObj, e, "wfPartnerChoice", s);
                oErr = s;
                return false;
            }
        }

        protected void bExport_Click(object sender, EventArgs e)
        {
            string Err = "";
            bool RetVal;
            RetVal = ExcelExport(Convert.ToInt32(tbPRT_ID.Text), 1, ref Err);
            if (!RetVal)
            {
                lMessage.Text = Err;
            }
            else
            {
                lMessage.Text = "";
            }
        }

        protected void ddlEv_SelectedIndexChanged(object sender, EventArgs e)
        {

            calPRT_SZUL_DATUM.SelectedDate = Convert.ToDateTime(ddlEv.SelectedValue + "." + calPRT_SZUL_DATUM.SelectedDate.Month.ToString() + "." + calPRT_SZUL_DATUM.SelectedDate.Day.ToString());
            calPRT_SZUL_DATUM.VisibleDate = Convert.ToDateTime(ddlEv.SelectedValue + "." + calPRT_SZUL_DATUM.SelectedDate.Month.ToString() + "." + calPRT_SZUL_DATUM.SelectedDate.Day.ToString());
            
            if (Convert.ToInt32(ddlPRT_TIPUS.SelectedValue) < 50)
            {
                lPRT_NEV.Text = "Veztéknév:";
                lPRT_NEV2.Text = "Keresztnév:";
                lPRT_SZUL_DATUM.Text = "Szül. dátuma";
            }
            else
            {
                lPRT_NEV.Text = "Megnevzés:";
                lPRT_NEV2.Text = "Rövidnév:";
                lPRT_SZUL_DATUM.Text = "Alapítás dátuma";
            }
        }

        protected void bVisszaFooldalra_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfDefault.aspx");
        }
        
    }
}
